package com.future

class UserController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
  def loginService
  def beforeInterceptor=[action:this.&loginValidate,except:['login','create','save']]
  def loginValidate={
      def loginFlag=session.getAttribute("loginFlag")
      if(!loginFlag){
        println "11"
         redirect(controller:'login',action:'index')
        return false
      }
    }
  def index = {
    redirect(action: "list", params: params)
  }
  def login={
    def pam=loginService.login(params)
    if(pam=="true"){
      session.setAttribute ("loginFlag",loginService.userInstance.userName)
      redirect(controller:"task",action:"list")
    }else{
      request.setAttribute ("error",pam)
      println pam
      def userInstance=new User(params)
      render(view:"login",model:[userInstance:userInstance])
    }
//     def userInstance=new User(params)
//     if(params.userName==null ||params.password==null){
//       render(view:"login")
//     }else{
//       if(User.countByUserName(params.userName)==1&&User.countByPassword(params.password)==1){
//         forward(controller:"task",action:"list")
//       }else{
//         flash.message="${message(code:'login.error')}"
//         render(view:"login")
//       }
//    }
  }
  def list = {
    params.max = Math.min(params.max ? params.int('max') : 10, 100)
    [userInstanceList: User.list(params), userInstanceTotal: User.count()]
  }

  def create = {
    def userInstance = new User()
    userInstance.properties = params
    return [userInstance: userInstance]
  }

  def save = {
    def userInstance = new User(params)
    userInstance.ip=getIp()//保存用户的ip地址
    userInstance.lastLoginDate=new Date()//初始化最后登录日期
    if (userInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), userInstance.userName])}"
      redirect(action: "index",controller:"login",params:[userInstance:userInstance])
    }
    else {
      render(view: "create", model: [userInstance: userInstance])
    }
  }

  def show = {
    def userInstance = User.get(params.id)
    if (!userInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
      redirect(action: "list")
    }
    else {
      [userInstance: userInstance]
    }
  }

  def edit = {
    def userInstance = User.get(params.id)
    if (!userInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [userInstance: userInstance]
    }
  }

  def update = {
    def userInstance = User.get(params.id)
    if (userInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (userInstance.version > version) {

          userInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'user.label', default: 'User')] as Object[], "Another user has updated this User while you were editing")
          render(view: "edit", model: [userInstance: userInstance])
          return
        }
      }
      userInstance.properties = params
      if (!userInstance.hasErrors() && userInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'user.label', default: 'User'), userInstance.id])}"
        redirect(action: "show", id: userInstance.id)
      }
      else {
        render(view: "edit", model: [userInstance: userInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def userInstance = User.get(params.id)
    if (userInstance) {
      try {
        userInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), params.id])}"
      redirect(action: "list")
    }
  }
}
